package common;

import dbhandler.Guest;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class SearchGuestDialog extends javax.swing.JDialog {

    private ArrayList<Guest> guests;
    private Guest guest;
    private SearchGuest searchGuest;
    private MouseAdapter mouseAdapter;
    private JPanel masterPanel;

    /**
     * Creates new form SearchGuestDialog
     * @param parent
     * @param modal
     */
    public SearchGuestDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        actionListeners();
        masterPanel = new JPanel();
    }

    /**
     *
     */
    private void search() {
        String fName = null;
        String mName = null;
        String lName = null;
        
        if(!fNameFld.getText().isEmpty()){
            String tempFName1 = fNameFld.getText().substring(1, fNameFld.getText().length()).toLowerCase();
            String tempFName2 = fNameFld.getText().substring(0,1).toUpperCase();
            fName = tempFName2 + tempFName1;
        }

        if(!mNameFld.getText().isEmpty()){
            mName = mNameFld.getText().substring(0, 1).toUpperCase();
        }

        if(!lNameFld.getText().isEmpty()){
            String tempLName1 = lNameFld.getText().substring(1, lNameFld.getText().length()).toLowerCase();
            String tempLName2 = lNameFld.getText().substring(0,1).toUpperCase();
            lName = tempLName2 + tempLName1;
        }
        searchGuest = new SearchGuest(fName, mName, lName);
        searchGuest.search();
        guests = searchGuest.getGuest();
        drawGuests();
    }
        
    /**
     *
     * @return
     */
    public Guest returnGuest() {
        this.setVisible(true);
        return guest;
    }

    /**
     *
     */
    private void drawGuests(){
        masterPanel.setPreferredSize(new Dimension(searchPanel.getViewport().getWidth(), guests.size()*20));
        searchPanel.getViewport().add(masterPanel);
        searchPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        searchPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        for(int i = 0; i < guests.size(); i++){
            JPanel panel = new JPanel();
            JLabel label = new JLabel();
            panel.setVisible(true);
            panel.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
            panel.setBackground(Color.WHITE);
            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            label.setVisible(true);

            masterPanel.setLayout(new FlowLayout(FlowLayout.LEADING,5,0));

            label.setText(guests.get(i).toString());

            panel.addMouseListener(mouseAdapter);
            panel.setPreferredSize(new Dimension(searchPanel.getWidth()-10,20));

            panel.add(label);
            masterPanel.add(panel);
        }
    }
    
    /**
     *
     */
    private void actionListeners(){
        mouseAdapter = (new java.awt.event.MouseAdapter() {
            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                panelMousePressed(evt);
            }
            public void panelMousePressed(MouseEvent evt){
                for (int i = 0; i < masterPanel.getComponentCount(); i++) {
                    JPanel tempPan = (JPanel) masterPanel.getComponent(i);
                    tempPan.setBackground(Color.WHITE);
                }
                JPanel panel = (JPanel) evt.getSource();
                JLabel label = (JLabel) panel.getComponent(0);
                panel.setBackground(Color.GREEN);

                for(int i = 0; i < guests.size(); i++){
                    if(guests.get(i).toString().equalsIgnoreCase(label.getText())){
                        guest = guests.get(i);
                    }
                }
            }

        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        infoPnl = new javax.swing.JPanel();
        fNameLbl = new javax.swing.JLabel();
        mNameLbl = new javax.swing.JLabel();
        lNameLbl = new javax.swing.JLabel();
        fNameFld = new javax.swing.JTextField();
        mNameFld = new javax.swing.JTextField();
        lNameFld = new javax.swing.JTextField();
        searchBtn = new javax.swing.JButton();
        submitBtn = new javax.swing.JButton();
        logoPnl = new javax.swing.JPanel();
        cancelBtn = new javax.swing.JButton();
        searchPanel = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Hotel Lazy BMS - Search Guest");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        infoPnl.setBackground(new java.awt.Color(255, 255, 255));
        infoPnl.setBorder(javax.swing.BorderFactory.createTitledBorder("Guest Information"));

        fNameLbl.setText("First Name :");

        mNameLbl.setText("Middle Name(s) : ");

        lNameLbl.setText("Last Name :");

        javax.swing.GroupLayout infoPnlLayout = new javax.swing.GroupLayout(infoPnl);
        infoPnl.setLayout(infoPnlLayout);
        infoPnlLayout.setHorizontalGroup(
            infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPnlLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fNameLbl, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lNameLbl, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(4, 4, 4)
                .addGroup(infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoPnlLayout.createSequentialGroup()
                        .addComponent(fNameFld, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(mNameLbl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mNameFld, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lNameFld, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE))
                .addContainerGap())
        );
        infoPnlLayout.setVerticalGroup(
            infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPnlLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fNameFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fNameLbl)
                    .addComponent(mNameLbl)
                    .addComponent(mNameFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(infoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lNameLbl)
                    .addComponent(lNameFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        searchBtn.setText("Search");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        submitBtn.setText("Submit");
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });

        logoPnl.setMinimumSize(new java.awt.Dimension(100, 50));
        logoPnl.setPreferredSize(new java.awt.Dimension(100, 50));

        javax.swing.GroupLayout logoPnlLayout = new javax.swing.GroupLayout(logoPnl);
        logoPnl.setLayout(logoPnlLayout);
        logoPnlLayout.setHorizontalGroup(
            logoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        logoPnlLayout.setVerticalGroup(
            logoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(searchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(searchBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(submitBtn))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(infoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(logoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(infoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(logoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(submitBtn)
                        .addComponent(cancelBtn)
                        .addComponent(searchBtn))
                    .addComponent(searchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addContainerGap())
        );

        logoPnl.add(new ImagePanel("logo.png"));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed
        search();
    }//GEN-LAST:event_searchBtnActionPerformed

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        dispose();
    }//GEN-LAST:event_submitBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        dispose();
    }//GEN-LAST:event_cancelBtnActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SearchGuestDialog dialog = new SearchGuestDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JTextField fNameFld;
    private javax.swing.JLabel fNameLbl;
    private javax.swing.JPanel infoPnl;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField lNameFld;
    private javax.swing.JLabel lNameLbl;
    private javax.swing.JPanel logoPnl;
    private javax.swing.JTextField mNameFld;
    private javax.swing.JLabel mNameLbl;
    private javax.swing.JButton searchBtn;
    private javax.swing.JScrollPane searchPanel;
    private javax.swing.JButton submitBtn;
    // End of variables declaration//GEN-END:variables
}